﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PBDesk.Framework.Web;
using PBDesk.Utilities;
using System.Text;
using CMS.PBDesk.Business.UserManager;
using System.Web.Security;
using System.Web.Profile;

namespace PBDesk.Web.CMS.usercontrols.PBDesk.Membership
{
    public partial class NewUser : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string gigyaUid = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "UID");
            string loginProvider = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "loginProvider");
            if (gigyaUid.IsNullOrEmpty() || loginProvider.IsNullOrEmpty())
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            string fName = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "firstName");
            string lName = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "lastName");
            string nickName = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "nickname");
            string email = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "email");
            string thumbnailURL = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "thumbnailURL");
            string displayName = CommonHelper.GetDisplayName(fName, lName, nickName, email);            

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Hello {0},<br/>Welcome to PBDesk.com!<br/><br/>", displayName);
            sb.AppendFormat("You are trying to sign-in using your <b>{0}</b> credentials for the first time. Please complete this form to create you account with this site.<br/><br/>", loginProvider);
            sb.AppendFormat("Once your PBDesk.com account is created, you can easily login just using your this social credentials. No need to remember any new user id or passsword.");
            ltlWelcomeMsg.Text = sb.ToString();

            if (loginProvider.IsNotNullAndNotEmpty())
                loginProviderImage.ImageUrl = CommonHelper.GetLoginProviderImage(loginProvider);

            if (loginProviderImage.ImageUrl.IsNullOrEmpty())
                loginProviderImage.Visible = false;

            ltlDisplayName.Text = displayName;

            if (thumbnailURL.IsNotNullAndNotEmpty())
            {
                profilePicImage.ImageUrl = thumbnailURL;
            }
            else
            {
                profilePicImage.ImageUrl = CommonHelper.GetDefailtProfilePicUrl();
            }

            if (email.IsNotNullAndNotEmpty() && StringUtils.IsValidEMailID(email))
            {
                EmailTextBox.Text = email;
                EmailTextBox2.Text = email;
            }

        }

        protected void SignupButton_Click(object sender, EventArgs e)
        {
            string gigyaUid = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "UID");
            string loginProvider = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "loginProvider");
            UserCreationStatus status = UserCreationStatus.failure;
            if (EmailTextBox.Text == EmailTextBox2.Text && StringUtils.IsValidEMailID(EmailTextBox.Text) && gigyaUid.IsNotNullAndNotEmpty())
            {
                status = UserManager.CreateUser(gigyaUid, EmailTextBox.Text);
            }
            else
            {
                
            }
            if (status != UserCreationStatus.success)
            {
                ErrLiteral.Text = "Sorry your membership cannot be processed at of this time. Error has been reoprted.";
                ErrLiteral.Visible = true;
            }
            else if(status == UserCreationStatus.success)
            {
                FormsAuthentication.SetAuthCookie(EmailTextBox.Text, false);
                var profile = ProfileHelper.GetProfile(EmailTextBox.Text, true);
                
                if (profile != null)
                {
                    string fName = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "firstName") ?? string.Empty;
                    string lName = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "lastName") ?? string.Empty;
                    string nickName = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "nickname") ?? string.Empty;
                    string displayName = CommonHelper.GetDisplayName(fName, lName, nickName, EmailTextBox.Text);
                    string thumbnailURL = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "thumbnailURL") ?? CommonHelper.GetDefailtProfilePicUrl();

                    profile.FirstName = fName;
                    profile.LastName =lName;
                    profile.DisplayName = displayName;
                    profile.LoginProvider = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "loginProvider") ?? string.Empty; ;
                    profile.LoginProviderUID = CookieManager.GetCookieValue(CookieHelper.COOKIE_GIGYA_INFO, "loginProviderUID") ?? string.Empty; ;
                    profile.AuthenticationProvider = "gigya";
                    profile.AuthenticationProviderUID = gigyaUid;
                    profile.ThumbnailUrl = thumbnailURL;
                    
                    profile.Save();

                }

                FormsAuthentication.RedirectFromLoginPage(EmailTextBox.Text, false);
            }

        }

       

       


    }
}